#include "efi_protocol.h"


int my_add(EFI_PRO *this,int a,int b)
{
    return (a + b);
}

int my_max(EFI_PRO *this,int a,int b)
{
    return (a>b?a:b);
}



EFI_PRO EFI_Pro = {
    my_add,
    my_max
};

//-----------------------
int Args_Init(EFI_ARGS *this,ARGS *arglist,int len)
{
    if (arglist == NULL) {
        printf("Argument error!\n");
        return ERROR;
    }

    arglist->str = (char *)malloc(len);
    if (arglist->str == NULL) {
        printf("No memory!\n");
        return ERROR;
    }

    memset(arglist->str,0,len);
    return OK;
}

static int Conver_Cmd(ARGS *arglist)
{
    int i,len;
    if (arglist ==NULL) {
        printf("Argument error!\n");
        return ERROR;
    }

    len = strlen(arglist->str);
    arglist->argc = 0;

    for (i=0;i<len;) {
        while (arglist->str[i] == ' ')i++;
        if (i == len) {
            break;
        }
        arglist->argv[arglist->argc++] = &arglist->str[i++];
        while (arglist->str[i] != ' ' && arglist->str[i] != '\0')i++;
        arglist->str[i++] = '\0';
    }

    return OK;
}

int Args_GetCmd(EFI_ARGS *this,ARGS *arglist)
{
    int c;
    int i = 0;

    if (arglist ==NULL) {
        printf("Argument error!\n");
        return ERROR;
    }

    ClearBuf();
        
    while ((c = getchar()) && c != '\n') {
        arglist->str[i++] = c;
        if (i == 100) {
            break;
        }
    }
    arglist->str[i] = '\0';

    return Conver_Cmd(arglist);

}

int Args_Length(EFI_ARGS *this,ARGS *arglist)
{
    if (arglist ==NULL) {
        printf("Argument error!\n");
        return 0;
    }

    return arglist->argc;
}

int Args_Display(EFI_ARGS *this,ARGS *arglist)
{
    int ix;

    if (arglist ==NULL) {
        printf("Argument error!\n");
        return ERROR;
    }

    for (ix=0;ix<this->Length(this,arglist);ix++) {
        printf("%s\n",arglist->argv[ix]);
    }
    return OK;
}

int Args_Delete(EFI_ARGS *this,ARGS *arglist)
{
    if (arglist ==NULL) {
        printf("Argument error!\n");
        return ERROR;
    }

    free(arglist->str);
    arglist->str = NULL;

    return OK;
}



EFI_ARGS Arg_Protocol = {
    Args_Init,
    Args_GetCmd,
    Args_Length,
    Args_Display,
    Args_Delete
};
